Ext.namespace('SilverMapper');

SilverMapper.Filter = function(config) {

	Ext.apply(this, config, {
			title: Lang.Get('Core','general','filter_title')
		,	labelWidth: 100
		,	url:'api/silvermapper.php'
		,	autoScroll: true
		,	bodyStyle:'padding:5px 5px 0'
		,	id: 'sm-filter'
		,	border: false
		,	maxSize: 250
		,	minSize: 250
		,	defaults: {width: 155}
		, hideMode: 'offsets'
		,	bbar: [{
					text: Lang.Get('Core','general','filter_reset')
				, iconCls: 'x-tbar-loading'
				,	scope: this
				, handler: function() {
						this.getForm().reset();
						Ext.getCmp('sm-map').getMarkers( 1 ); 
					}
			}]
		,	items: [{			
					xtype: 'xtwincombo'
				, fieldLabel: 'Invasive Species'
	      , store: new Ext.data.SimpleStore({
		        	fields: [ 'Invasive_Species' ]
	        	,	data: invasive_species
    			})
        ,	displayField: 'Invasive_Species'
        ,	typeAhead: true
        ,	mode: 'local'
        ,	forceSelection: true
        ,	triggerAction: 'all'
        ,	selectOnFocus: true
				,	name: 'Invasive_Species'
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
	    },	{
					xtype: 'xtwincombo'
				, fieldLabel: 'Noxious Weeds'
	      , store: new Ext.data.SimpleStore({
		        	fields: [ 'Noxious_Weeds' ]
	        	,	data: noxious_weeds
    			})
        ,	displayField: 'Noxious_Weeds'
        ,	typeAhead: true
        ,	mode: 'local'
        ,	forceSelection: true
        ,	triggerAction: 'all'
        ,	selectOnFocus: true
				,	name: 'Noxious_Weeds'
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}					
	    },	{
				
					xtype: 'xtwincombo'
				, fieldLabel: 'Family'
				, name: 'Family'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "Family" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'Genus'
				, name: 'Genus'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "Genus" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'Species'
				, name: 'SpeciesEpithet'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "SpeciesEpithet" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'Var/Ssp'
				, name: 'TrinomialEpithet'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "SpeciesEpithet" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'Common Name'
				, name: 'CommonNames'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "CommonNames" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}					
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'Collector'
				, name: 'Collector'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "Collector" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers
						,	'clear': this.getMarkers
					}
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'County'
				, name: 'County'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "County" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers
						,	'clear': this.getMarkers
					}
			}, {				
					xtype: 'component'
				, autoEl: { 
							tag: 'div'
						, cls: 'form-note'
						, html: 'Note: You will not be able to mix Noxious Weeds, Invasive Species, or (Family, Genus, Species) in the same search.'
					}
			}]		
		,	listeners: {
				'show': function() {
					if ( this.fix ) {
						this.fix = false;
					}
				}
			}
	});

	SilverMapper.Filter.superclass.constructor.apply(this, arguments);

};

Ext.extend( SilverMapper.Filter, Ext.FormPanel, {

		getMarkers: function() {
			Ext.getCmp('sm-map').getMarkers( 1 ); 
		}

	,	getMarkers2: function( f_item ) {
		
		var form = Ext.getCmp('sm-filter').getForm();

		switch( f_item.name ) {
			case 'Invasive_Species':
				form.findField('Noxious_Weeds').reset();
				form.findField('Family').reset();
				form.findField('Genus').reset();
				form.findField('SpeciesEpithet').reset();
				form.findField('TrinomialEpithet').reset();
				form.findField('CommonNames').reset();
				break;

			case 'Noxious_Weeds':
				form.findField('Invasive_Species').reset();
				form.findField('Family').reset();
				form.findField('Genus').reset();
				form.findField('SpeciesEpithet').reset();
				form.findField('TrinomialEpithet').reset();
				form.findField('CommonNames').reset();
				break;

			case 'Family':
			case 'Genus':
			case 'SpecificEpithet':
			case 'SpeciesEpithet':
			case 'TrinomialEpithet':
			case 'CommonNames':
				form.findField('Noxious_Weeds').reset();
				form.findField('Invasive_Species').reset();
				break;
				
		}
		
		Ext.getCmp('sm-map').getMarkers( 1 ); 
	}

});

Ext.reg( 'sm-filter', SilverMapper.Filter ); 
